﻿using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using LightCAD.Core;
using LightCAD.Runtime.Editor;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LightCAD.Runtime
{
    public  class RuntimeScene
    {
        public static List<ControlGrip> ElementGrips = new List<ControlGrip>();
        public static ControlGrip SelectedGrip;
        public static ControlGrip HoveredGrip;

        public static void ResetElementGrips(List<Element> elements)
        {
            foreach (Element element in elements)
            {
                var grips = element.GetControlGrips();
                ElementGrips.AddRange(grips);
            }
        }

        public static void ClearElementGrips(List<Element> elements)
        {
            foreach (Element element in elements)
            {
                for (var i = ElementGrips.Count - 1; i >= 0; i--)
                {
                    if (ElementGrips[i].Element == element)
                    {
                        ElementGrips.RemoveAt(i);
                    }
                }
            }
        }
        public static void ClearElementGrips()
        {
            ElementGrips.Clear();
        }

        public static void DrawElementGrips(SKCanvas canvas)
        {
            //多线程需要考虑动态变化
            var count = ElementGrips.Count;
            for (var i=0;i<count;i++)
            {
                if (i > ElementGrips.Count - 1) break;
                ElementGrips[i].Draw(canvas);
            }
        }
       
        public static ControlGrip HitSelectGrip(Vector2d hitPoint,double selectSize)
        {
            SelectedGrip = null;
            for (var i = ElementGrips.Count - 1; i >= 0; i--)
            {
                var grip = ElementGrips[i];
                var testBox = new Box2d(new Vector2d(hitPoint.X - selectSize, hitPoint.Y - selectSize), selectSize * 2, selectSize * 2);
                var selected = grip.IntersectWithBox(testBox);
                if (selected)
                {
                    grip.IsSelected = true;
                    SelectedGrip = grip;
                    return grip;
                }
            }
            return null;
        }
        public static ControlGrip HoverSelectGrip(Vector2d hitPoint, double selectSize)
        {
            HoveredGrip = null;
            for (var i = ElementGrips.Count - 1; i >= 0; i--)
            {
                var grip = ElementGrips[i];
                var testBox = new Box2d(new Vector2d(hitPoint.X - selectSize, hitPoint.Y - selectSize), selectSize * 2, selectSize * 2);
                var hovered = grip.IntersectWithBox(testBox);
                if (hovered)
                {
                    grip.IsHovered = true;
                    HoveredGrip = grip;
                    return grip;
                }
            }
            return null;
        }

       

        public static void HoverUnselectGrip()
        {
            if (HoveredGrip != null)
            {

                HoveredGrip.IsHovered = false;
                HoveredGrip = null;
            }
        }

        public static Action<SKCanvas> DrawDraggingGrip;
        public static void DraggingGrip(Vector2d pw)
        {
            var ele = SelectedGrip.Element;
            DrawDraggingGrip = ele.SetDragGrip(SelectedGrip.Name, pw,false);
        }

        public static void DraggingGripEnd(Vector2d pw)
        {
            var ele = SelectedGrip.Element;
            ele.SetDragGrip(SelectedGrip.Name, pw,true);
            ele.ResetBoundingBox();
            DrawDraggingGrip = null;
            ClearElementGrips(new List<Element> { ele });
            ResetElementGrips(new List<Element> { ele });
        }
    }
}
